/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.toHouse.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.suli.toHouse.domain.ReceiptDetail;
import me.zhengjie.suli.toHouse.service.ReceiptDetailService;
import me.zhengjie.suli.toHouse.service.dto.ReceiptDetailQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @website https://eladmin.vip
* @author wyh
* @date 2024-02-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "入库管理")
@RequestMapping("/api/receiptDetail")
public class ReceiptDetailController {

    private final ReceiptDetailService receiptDetailService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('receiptDetail:list')")
    public void exportReceiptDetail(HttpServletResponse response, ReceiptDetailQueryCriteria criteria) throws IOException {
        receiptDetailService.download(receiptDetailService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询入库")
    @ApiOperation("查询入库")
    @PreAuthorize("@el.check('receiptDetail:list')")
    public ResponseEntity<Object> queryReceiptDetail(ReceiptDetailQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(receiptDetailService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增入库")
    @ApiOperation("新增入库")
    @PreAuthorize("@el.check('receiptDetail:add')")
    public ResponseEntity<Object> createReceiptDetail(@Validated @RequestBody ReceiptDetail resources){
        return new ResponseEntity<>(receiptDetailService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改入库")
    @ApiOperation("修改入库")
    @PreAuthorize("@el.check('receiptDetail:edit')")
    public ResponseEntity<Object> updateReceiptDetail(@Validated @RequestBody ReceiptDetail resources){
        receiptDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除入库")
    @ApiOperation("删除入库")
    @PreAuthorize("@el.check('receiptDetail:del')")
    public ResponseEntity<Object> deleteReceiptDetail(@RequestBody String[] ids) {
        receiptDetailService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/getReceiptDetailsByReceiptId/{id}")
    @Log("根据ID查询收货明细")
    @ApiOperation("根据ID查询收货明细")
    public ResponseEntity<Object> getReceiptDetailsById(@PathVariable String id) {
        return new ResponseEntity<>(receiptDetailService.getReceiptDetailsById(id), HttpStatus.OK);
    }
}